/**
 * This file is a part of qloudgen-sec. 
 * You can redistribute qloudgen-sec and/or modify it under the terms of the Lesser GNU General Public License version 3. 
 * qloudgen-sec is distributed WITHOUT ANY WARRANTY. 
 *
 * See the Lesser GNU General Public License for more details at http://www.gnu.org/licenses/. 
 **/
package org.qloudgen.sec.cas.server.authentication.handler.support;

import org.jasig.cas.authentication.handler.support.AbstractUsernamePasswordAuthenticationHandler;
import org.jasig.cas.authentication.UsernamePasswordCredential;
import org.jasig.cas.authentication.PreventedException;
import org.jasig.cas.authentication.principal.Principal;
import org.jasig.cas.authentication.principal.SimplePrincipal;
import org.jasig.cas.authentication.HandlerResult;

import org.qloudgen.sec.cas.support.user.services.User;
import org.qloudgen.sec.cas.support.user.services.UsersManager;

import java.util.Map;
import java.security.GeneralSecurityException;
import javax.validation.constraints.NotNull;

import javax.security.auth.login.AccountNotFoundException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * <p>Title: MongoDBAuthenticationHandler</p>
 * <p>Description: MongoDBAuthenticationHandler.</p>
 * <p>Copyright: qloudgen/sec 2011</p>
 * @author <a href="mailto:mikemaojun@gmail.com">mike</a>
 */

public class MongoDBAuthenticationHandler extends AbstractUsernamePasswordAuthenticationHandler {
	private static final Logger logger = LoggerFactory.getLogger(MongoDBAuthenticationHandler.class);
    
    /** Instance of dao. */
    @NotNull
    private UsersManager usersManager;
    
    /*public MongoDBAuthenticationHandler(){
    		  
    }*/
    
    public MongoDBAuthenticationHandler(final UsersManager usersManager) {
    	  this.usersManager = usersManager;
    	  logger.warn("{} init.............",getClass().getName());
	  }
	
	/**	
	protected Principal authenticateUsernamePasswordInternal(String username, String password) throws GeneralSecurityException, PreventedException{
    	return new SimplePrincipal(getUserInformation(username,password).getName());
    }*/
    
    protected HandlerResult authenticateUsernamePasswordInternal(final UsernamePasswordCredential transformedCredential) throws GeneralSecurityException, PreventedException {
    	final String username = transformedCredential.getUsername();
    	final String encodedPassword = this.getPasswordEncoder().encode(transformedCredential.getPassword());
    	
    	User user = getUserInformation(username,encodedPassword);
    	
    	//Map attributes = user.getCol();
    	
        logger.debug("user",user.toString());
        //logger.debug("user attribute {}",attributes);
    	
    	if (user == null){
    		throw new AccountNotFoundException(username + " not found in system.");
    	}
    	return createHandlerResult(transformedCredential, new SimplePrincipal(username), null);
    }

	/**
	 * Get user information from the database according to the username and password provided.
	 * @param username,password
	 * @return the user information object
	 */
	private User getUserInformation(String username,String password){
		User user = null;
		
		user = usersManager.authenticationUserBy(username,password);
		
		logger.debug("********************");
		logger.debug("authenticate user is:" + username + password);
		logger.debug("********************");
		
		return user;
	}
		
	public void setUsersManager(UsersManager usersManager){
		
		 this.usersManager = usersManager;
	}
	
	public UsersManager getUsersManager(){
		
		 return this.usersManager;
	}
}
